//
//  ViewController.m
//  Homework_14
//
//  Created by 徐昊 on 16/8/2.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"
#import "CustomCollectionReusableView.h"
#import "HeaderCollectionReusableView.h"
#import "ImageCollectionViewCell.h"
#define kCollectionViewCellName @"reuse"
#define kCollectionViewHeaderCellName @"reuseHeader"
#define kCollectionViewCustomHeaderCellName @"reuseCustomHeader"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"攻略", @"单品"]];
    self.navigationItem.titleView = seg;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((CGRectGetWidth(self.view.bounds) - 30) / 2, 120);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.headerReferenceSize = CGSizeMake(0, 50);
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [collectionView registerClass:[ImageCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewCellName];
    [collectionView registerClass:[CustomCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderCellName];
    [self.view addSubview:collectionView];
    [collectionView registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewCustomHeaderCellName];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
        return 10;
    }
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellName forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    return cell;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HeaderCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewCustomHeaderCellName forIndexPath:indexPath];
        return view;
    } else {
        CustomCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderCellName forIndexPath:indexPath];
        view.label.text = @"品类";
        [view.button setTitle:@"查看全部 >" forState:UIControlStateNormal];
        return view;
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLanuyout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(0, 400);
    } else {
        return CGSizeMake(0, 50);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
